package de.lns.entities.turrets;

import java.nio.FloatBuffer;
import java.util.ArrayList;

import de.lns.entities.Entity;
import de.lns.entities.Hitbox;
import de.lns.entities.ai.Bulletai;
import de.lns.entities.ai.StraightBulletai;
import de.lns.entities.effect.Effect;
import de.lns.entities.effect.Effectable;
import de.lns.entities.mobs.Mob;
import de.lns.level.Circle;
import de.lns.level.Level;
import de.lns.level.Rectangle;
import de.lns.level.Shapes;
import de.lns.render.ModelFactory;
/**
 * 
 * @author Jascha
 *
 */
public abstract class Bullet extends Entity {
	
	protected static FloatBuffer mesh = ModelFactory.genRectMesh(1, 1, 1/2 , 1/2);
	protected Mob target;
	protected Bulletai ai;
	protected double speed=400;
	/**
	 * 
	 * @return The movementspeed of this Bullet.
	 */
	public double getSpeed() {
		return speed;
	}
	/**
	 * 
	 * @param speed Determines how fast this bullet travels in tiles per second.
	 */
	public void setSpeed(double speed) {
		this.speed = speed;
	}
	/**
	 * 
	 * @param posX The XPosition of this bullet.
	 * @param posY The YPosition of this bullet.
	 * @param posZ The ZPosition of this bullet.
	 * @param hitZ The Hitbox in Z-Direction(height).
	 */
	public Bullet(double posX, double posY, double posZ, double hitZ) {
		super(posX, posY, posZ, hitZ);
		this.hitbox=new Circle(this,1);
	}
	/**
	 * 
	 * @param posX The XPosition of this bullet.
	 * @param posY The YPosition of this bullet.
	 */
	public Bullet(double posX, double posY, Mob target) {
		super(posX, posY);
		hitbox = new Circle(this, 1);
		this.target = target;
		
	}
	/**
	 * 
	 * @param target The target of the bullet (used for homing bullets or first traveldirection).
	 */
	public void setTarget(Mob target){
		this.target=target;
	}
	/**
	 * 
	 * @return The Target of this bullet.
	 */
	public Mob getTarget(){
		return target;
	}
	
	/**
	 * If the bullet collides with anything it adds the own effect onto that mob.
	 * @param mob The Mob the bullet did collide with.
	 */
	public abstract void onCollision(Mob mob);
	/**
	 * The bullet moves.
	 */
	public void update() {
		
		if(!Level.getEntityList().contains(target) && !(ai instanceof StraightBulletai)){
			Level.removeEntity(this);
		}
		ai.move();
	}
	public FloatBuffer getMesh() {
		return this.mesh;
	}

	@Override
	public FloatBuffer getMapping() {
		return this.map;
	}
	@Override
	public boolean alignRel() {
		return true;
	}
	
	public void checkHit() {
		for (Mob mob:Level.getMobs()){
			if(Hitbox.checkhit(getHitbox(),mob.getHitbox())){
				onCollision(mob);
				break;
			}
		}
	}
	
	public void removeBullet(){
		Level.removeEntity(this);
	}

}